/**
 * 帮助详情模块
 * @author
 * @date 2012-11-20
 */
var tag=false;
var qid=window.location.hash.split("=")[1];
var href= window.location.href;
if(href.indexOf("searchDetail")){
qid=window.location.href.split("=")[1];
tag=true;
}
var colNames = ['','photo','ID','回答人','回答时间','回答内容'];

/**
 * colModel 显示列的属性
 *
 * @type {Object}
 */
var colModel = [
	{ name:'first', index:'first', width:"100%", align:"left" ,title:false},
	{ name:'replierPortrait',index:'replierPortrait', hidden:true },
	{ name:'id',index:'id', hidden:true },
	{ name:'replierName',index:'replierName', hidden:true },
	{ name:'formatPublishTime',index:'formatPublishTime', hidden:true },
	{ name:'content',index:'content', hidden:true }
];

/**
 * rowNum 想要显示的数据行数
 *
 * @type {Number}
 */

var rowNum = 10;

/**
 * total_last 用于判定表格的总页码是否发生了改变
 *
 * @type {Number}
 */

var total_last = 0;

/**
 * loadDataSuccess 判定是否成功加载完数据
 *
 * @type {Boolean}
 */

var loadDataSuccess = false;

$( "#help-detail-table" ).jqGrid({
	url:'../help/loadQuestionsByQuestionId.html?questionId='+qid,
	datatype:"json",
	mtype: "POST",
	height: "auto",
	autowidth: true,
	colNames: colNames, 
	colModel: colModel,
	rowNum: rowNum, 
	viewrecords: true,
	emptyrecords : '暂无人回复,赶紧抢沙发<br>',
	pager: 'helpDetailpager', 
	jsonReader: {
		root: "result",
		page: "page",
		total: "total",
		records: "records",
		repeatitems: false,
		cell: "cell",
		id: "id"
	},
	
	gridComplete:function() {
		// 取消外边框
		$( "#gbox_help-detail-table" ).css( "border", "none" );
		
		// 隐藏表头
		$( "#gview_help-detail-table .ui-jqgrid-hdiv" ).css( "display", "none" );
	
		// 取消列间的分界限
		$( "#gview_help-detail-table .ui-jqgrid-bdiv td" ).css( "border-right-style", "none" );

		// 改变底边框样式
		$( "#gview_help-detail-table .ui-jqgrid-bdiv tr:not(:first-child) td" ).css( "border-bottom", "1px solid #E5E5E5" );
		
		// 取消最后一行的底边框
		$( "#gview_help-detail-table .ui-jqgrid-bdiv tr:last-child td" ).css( "border-bottom-style", "none" );
		
		// 设置表格行高
		//$( "#gview_help-detail-table .ui-jqgrid-bdiv tr:gt(0)" ).css( "height", "110px" );

		/**
		 * IDs 获取当前页所有数据的id
		 *
		 * @type {Object}
		 */

		var IDs = $( "#help-detail-table" ).jqGrid( "getDataIDs" );
		//没有数据
		if(IDs.length==0){
			$("#helpDetailpager_center").hide();
			$(".ui-paging-info").css("text-align","left");
			$(".ui-paging-info").css("font-size","15px");
			$(".ui-jqgrid-view").css("height","30px");
		}
		
		for (var i=0; i < IDs.length; i++) {

			/**
			 * everyRowData 根据某一行数据的id获取该数据所有信息
			 *
			 * @type {Object}
			 */

			var everyRowData = $( "#help-detail-table" ).jqGrid( "getRowData", IDs[i] );
			
			// 回答内容自定义模块
			var metadata2 ='<div class="answer2">' +
								'<div class="p-photo"><img src="../'+everyRowData.replierPortrait+'" width="50" height="50" /></div>'+
								'<div class="p-text">'+
									'<div class="floor-info">' +
										'<span class="name">' +									
											'<span class="num">' + (i+1) + '</span>楼：' + everyRowData.replierName + 
											'<span class="tx-thin2" style="display:none">（产品经理）</span>' +
										'</span> ' +
										'<span class="time">于' + everyRowData.formatPublishTime +'发表</span>' +
									'</div>' +
									'<div class="comment">' +
										'<p>' + everyRowData.content +'</p>' +						
									'</div>' +
								'</div>' +	
							'</div>' ;
							
			$( "#help-detail-table" ).jqGrid( 'setRowData', IDs[i], { first: metadata2 } );
			// 取消提示信息
			$( ".metadata" ).parent("td").attr("title","");
			
        }
	
	}
});

//点击返回，触发事件
$("#help-return").click(function(){

	//清空
	$(".help-content").empty();
	$(".help-content").load(
		"../help/helpCenter.html", 
		null, 
		function(response, status, xml) {
			if ( status !== "success" ) {
				alert("加载失败！");
			}else{
				$("#search").show();
			}
		}
	
	);}	
);
$("#delete").click(function(){
	if ( confirm( "您确认要删除这个问题吗?" ) ) {
	var datastr="id="+qid;
	$.ajax({
		url : '../help/deleteQuestion.html',
		data : datastr,
		type : 'post',
		dataType : 'json',
		contentType : "application/x-www-form-urlencoded;charset=utf-8",
		success : function(result) {
				if(tag){
					alert("删除成功");
					//window.close();
					location.reload();
				}else{
					alert("删除成功");
					$(".help-content").empty();
					$(".help-content").load(
					"../help/helpCenter.html", 
					null, 
					function(response, status, xml) {
						if ( status !== "success" ) {
							alert("加载失败！");
						}
					}
				);	
				}
		},

		error : function() {
			alert("连接服务器出错");
		}
	});
	//清空
	}
});

//点击回答触发事件
$("#help-answer").click(function(){
	//获取焦点
	$(".answer-area").focus();	
});

//提问的内容隐藏
var pnum = parseInt($(".ui-pg-input").val());
$(".ui-icon-seek-next, .ui-icon-seek-end").click(function(){	
	if( pnum > 0 ){
		$("#dis-content").hide(200);
	}
});
//提问的内容显示
$(".ui-icon-seek-prev, .ui-icon-seek-first").click(function(){
	if( pnum <= 1 ){
		$("#dis-content").show(200);
	}
});
$("#reply").click(function(){
	var datastr="";
	var content=$("#replyContent").val();
	datastr="questionId="+qid+"&content="+content;
	
	alert(datastr);
	
	$.ajax({
		url : '../help/saveReply.html',
		data : datastr,
		type : 'post',
		dataType : 'json',
		contentType : "application/x-www-form-urlencoded;charset=utf-8",
		success : function(result) {
			if(result=="true"){
				$( "#help-detail-table" ).trigger( "reloadGrid" );
				$("#replyContent").val("");
			}
			else if(result=="USER_NOT_LOGIN"){
				alert("请先登录");
				var url = window.location.href;
				window.location.href="../base/login.html?url="+url;
			}
			
		},

		error : function() {
			alert("连接服务器出错");
		}
	});
});

